package com.hzqy.service.epg;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.epg.AcBFRecordDao;
import com.hzqy.web.vo.epg.AcBFConfigVo;
import com.hzqy.web.vo.epg.AcBFRecordVo;


@Service
public class AcBFRecordServiceBean {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private AcBFRecordDao acBFRecordDao;
	
	public Map<String, Object> selectAcBFRecord(AcBFRecordVo acrecord){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("f_ar_serverid", acrecord.getF_ar_serverid());
		map.put("f_ar_activityid", acrecord.getF_ar_activityid());
		map.put("f_ar_periods", acrecord.getF_ar_periods());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		String today = sdf.format(d);
		map.put("f_ar_cdate", today);
		AcBFRecordVo acBFRecordVo = acBFRecordDao.selectAcBFRecordByService(map);
		Map<String, Object> map2 = new HashMap<String, Object>();
		if (acBFRecordVo == null) {
			map2.put("flag", "error");
		}else {
			map2.put("flag", "success");
			map2.put("result", acBFRecordVo);
		}
		return map2;
	}
	
	public Map<String, Object> insertAcBFRecordBy(AcBFRecordVo acrecord){
		Map<String, Object> map3 = new HashMap<String, Object>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		map3.put("f_ar_serverid", acrecord.getF_ar_serverid());
		map3.put("f_ar_activityid", acrecord.getF_ar_activityid());
		map3.put("f_ar_periods", acrecord.getF_ar_periods());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		String today = sdf.format(d);
		map3.put("f_ar_cdate", today);
		AcBFRecordVo acBFRecordVo = acBFRecordDao.selectAcBFRecordByService(map3);
		if(acBFRecordVo != null) {
			map2.put("flag", "error");
			return map2;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("f_ac_activityid", acrecord.getF_ar_activityid());
		map.put("f_ac_periods", acrecord.getF_ar_periods());
		map.put("f_ac_cdate", today);
		AcBFConfigVo Config = acBFRecordDao.selectConfig(map);
		if (Config != null) {
			if (Config.getF_ac_code().equals(acrecord.getF_ar_target())) {
				acrecord.setF_ar_type("1");
			}else {
				acrecord.setF_ar_type("2");
			}
			acrecord.setF_ar_name(Config.getF_ac_name());
		}else {
			acrecord.setF_ar_type("2");
			acrecord.setF_ar_name("");
		}
		
		int flag = acBFRecordDao.insertAcBFRecordBy(acrecord);
		
		if (flag > 0) {
			map2.put("flag", "success");
		}else {
			map2.put("flag", "error");
		}
		return map2;
	}
	
	public List<AcBFRecordVo> selectByCdate(AcBFRecordVo acBFRecordVo){
		List<AcBFRecordVo> list = acBFRecordDao.selectByDate(acBFRecordVo);
		return list;
	}
	
	public Map<String, Object> insertRecord(AcBFRecordVo acBFRecordVo){
		Map<String, Object> map2 = new HashMap<String, Object>();
		int flag = acBFRecordDao.insertAcBFRecordBy(acBFRecordVo);
		if (flag > 0) {
			map2.put("flag", "success");
		}else {
			map2.put("flag", "error");
		}
		return map2;
	}
	
	public List<AcBFRecordVo> selectCount(AcBFRecordVo acBFRecordVo,int startNow,int pageSize){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("f_ar_activityid", acBFRecordVo.getF_ar_activityid());
		map.put("f_ar_serverid", acBFRecordVo.getF_ar_serverid());
		map.put("type", acBFRecordVo.getType());
		map.put("firstResult", startNow);
		map.put("maxResults", pageSize);
		List<AcBFRecordVo> list = acBFRecordDao.selectServerCount(map);
		return list;
	}

}
